
var scene, light, camera, mesh, geometry, renderer;
var canvas, container, stats, monitor;
var width = window.innerWidth,
  height = window.height;
var cameraParams = {},
  lightParams = {};

function initEnv() {
  container = document.getElementById("container");
  width = window.innerWidth;
  height = window.innerHeight;

  camera = new THREE.PerspectiveCamera(45, width / height, 1, 2000);

  light = new THREE.AmbientLight(0x101030);
  scene = new THREE.Scene();

  renderer = new THREE.WebGLRenderer({ antialias: false });
  var directionalLight = new THREE.DirectionalLight(0xffeedd);
  directionalLight.position.set(0, 0, 1);
  
  canvas = renderer.domElement;
  scene.add(directionalLight);
  scene.add(light);
  // renderer.setClearColor(0x000000);
  renderer.setPixelRatio(window.devicePixelRatio);
  renderer.setSize(width, height);
  container.appendChild(canvas);
}

function initStatus() {
  scene.add(new THREE.AmbientLight(0x444444));

  stats = new Stats();
  stats.domElement.style.position = "absolute";
  stats.domElement.style.top = "0px";
  container.appendChild(stats.domElement);
  container.appendChild(renderer.domElement);
}

function initMonitor() {
  // lightParams.x = 0;
  // lightParams.y = 0;
  // lightParams.z = 0;
  // lightParams.decay = 1;

  cameraParams.up = {};
  cameraParams.x = 0;
  cameraParams.y = 0;
  cameraParams.z = 100;
  // cameraParams.up.x = 0;
  // cameraParams.up.y = 1;
  // cameraParams.up.z = 0;

  monitor = new DAT.GUI();
  monitor.add(cameraParams, "x", -2000, 2000).name("相机X的位置");
  monitor.add(cameraParams, "y", -2000, 2000).name("相机Y的位置");
  monitor.add(cameraParams, "z", -2000, 5000).name("相机Z的位置");
  // monitor.add(cameraParams.up, "x", -1, 1).name("相机up.x的位置");
  // monitor.add(cameraParams.up, "y", -1, 1).name("相机up.y的位置");
  // monitor.add(cameraParams.up, "z", -1, 1).name("相机up.z的位置");

  // monitor.add(lightParams, "x", -2000, 2000).name("点光源X的位置");
  // monitor.add(lightParams, "y", -2000, 2000).name("点光源Y的位置");
  // monitor.add(lightParams, "z", -2000, 2000).name("点光源Z的位置");
  // monitor.add(lightParams, "decay", 0, 1).name("衰减");
}

function initModel() {
  var manage = new THREE.LoadingManager(),
    objLoader = new THREE.OBJLoader(manage),
    imgLoader = new THREE.ImageLoader(manage),
    texture = new THREE.Texture();

  imgLoader.load("../textures/UV_Grid_Sm.jpg", function (image) {
    console.log("image", image)
    texture.image = image;
    texture.needsUpdate = true;
  }, function () {
    console.log("image progress...", arguments);
  }, function () {
    console.log("image err", arguments)
  })

  objLoader.load("../model/male02.obj", function (obj) {
    console.log("obj", obj)
    obj.traverse(function (child) {
      if (child instanceof THREE.Mesh) {
        child.material.map = texture;
      }
      console.log("child",child)
    });
    obj.position.y = - 80;
    scene.add(obj);
  }, function () {
    console.log("progress...", arguments);
  }, function () {
    console.log("err", arguments)
  })
}

function initTexture() {

}

function updateEnv() {
  camera.position.x = cameraParams.x;
  camera.position.y = cameraParams.y;
  camera.position.z = cameraParams.z;
  camera.updateProjectionMatrix();
}

function render() {

  // var time = Date.now() * 0.001;

  // mesh.rotation.x = time * 0.25;
  // mesh.rotation.y = time * 0.5;
  camera.lookAt( scene.position );
  renderer.render(scene, camera);

}

function animate() {
  updateEnv();
  render();
  stats.update();
  requestAnimationFrame(animate);
}

function main() {
  initEnv();
  initStatus();
  initMonitor();
  initModel();
  animate();
}